package cn.fsd.service.impl;

import cn.fsd.domain.Commodity;
import cn.fsd.domain.Purchase_bill;
import cn.fsd.domain.Storeroom;
import cn.fsd.domain.Suppliers;
import cn.fsd.mapper.Purchase_billMapper;
import cn.fsd.query.delivery.PurchaseBillQuery;
import cn.fsd.service.PurchaseBillService;
import cn.fsd.util.Constants;
import cn.fsd.vo.PageBean;
import cn.fsd.vo.PurchaseBillBean;
import cn.fsd.vo.storeroom.purchase.PurchaseBillCommodityVO;
import cn.fsd.vo.StoreRoomCommodityBean;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PurchaseBillServiceImpl implements PurchaseBillService {

  @Autowired
  Purchase_billMapper billMapper;
  @Autowired
  SupplierServiceImpl supplierService;
  @Autowired
  CommodityServiceImpl commodityService;
  @Autowired
  StoreRoomServiceImpl storeRoomService;
  @Autowired
  StoreRoomCommodityServiceImpl storeRoomCommodityService;

  @Override
  public String addNewPurchaseBill(Purchase_bill bill) {
    String id = UUID.randomUUID().toString();
    bill.setBill_id(id);
    bill.setPurchase_status(0);
    bill.setReturn_num(0);
    billMapper.insert(bill);
    return id;
  }

  @Override
  public PageInfo<PurchaseBillCommodityVO> getPurchaseBillByPage(PurchaseBillQuery query) {
    PageHelper.startPage(query.getPageNow(), query.getPageSize());
    return new PageInfo<>(billMapper.selectByPage(query));
  }

  @Override
  public void changePurchase_status(Purchase_bill purchase_bill) {
    Purchase_bill record = billMapper.selectByPrimaryKey(purchase_bill.getBill_id());
    record.setPurchase_status(purchase_bill.getPurchase_status());
    billMapper.updateByPrimaryKey(record);
  }

  @Override
  public Purchase_bill getPurchase_billById(String bill_id) {
    return billMapper.selectByPrimaryKey(bill_id);
  }

  @Override
  public PageBean<Purchase_bill> queryBills(PurchaseBillQuery query) {
    PageBean<Purchase_bill> pageBean = null;
    PageInfo<Purchase_bill> pageInfo = null;
    if (Objects.equals(query.getSelectBy(), Constants.SELECTBYSUPPLIER)) {
      pageBean = new PageBean<>();
      List<Suppliers> suppliersList = supplierService.getSuppliersByName(query.getQuery());
      PageHelper.startPage(query.getPageNow(), query.getPageSize());
      List<Purchase_bill> bills = billMapper.selectBySuppliers(query, suppliersList);
      pageInfo = new PageInfo<>(bills);
    } else if (Objects.equals(query.getSelectBy(), Constants.SELECTBYCODE)) {
      pageBean = new PageBean<>();
      List<Commodity> commodities = commodityService.findCommodityByCode(query.getQuery());
      PageHelper.startPage(query.getPageNow(), query.getPageSize());
      List<Purchase_bill> bills = billMapper.selectByCommodityCode(query, commodities);
      pageInfo = new PageInfo<>(bills);
    }
    if (pageInfo == null) {
      return null;
    }
    pageInfo.setList(pageInfo.getList().parallelStream().map(item -> {
      PurchaseBillBean bean = new PurchaseBillBean(item);
      Storeroom storeroom = storeRoomService.getStoreRoomByID(bean.getStoreroom_id());
      // TODO: 2023/7/4 需要调用storeroom-commodity相关的接口
      StoreRoomCommodityBean storeroomCommodity = (StoreRoomCommodityBean) storeRoomCommodityService.getCommodityById(
          bean.getCommodity_id(), bean.getStoreroom_id());
      Suppliers supplier = supplierService.getSupplierByID(bean.getSupplier_id());
      if (storeroom != null) {
        bean.setStoreroom_name(storeroom.getStoreroom_name());
      }
      if (storeroomCommodity != null) {
        bean.setCommodity_code(storeroomCommodity.getCommodityCode());
        bean.setTransferable_num(storeroomCommodity.getTransferable_num());
      }
      if(supplier!=null){
        bean.setSupplier_name(supplier.getSupplier_name());
      }
      return bean;
    }).collect(Collectors.toList()));
    pageBean.setTotalPages(pageInfo.getPages());
    pageBean.setTotalRows(pageInfo.getTotal());
    pageBean.setRowData(pageInfo.getList());
    return pageBean;
  }

  @Override
  public int returnGoods(Purchase_bill bill) {
    Purchase_bill oldBill = billMapper.selectByPrimaryKey(bill.getBill_id());
    if(oldBill==null||Objects.equals(oldBill.getPurchase_status(),Constants.PURCHASEIN)){
      return -1;
    }
    oldBill.setReturn_num(oldBill.getReturn_num()==null?0:oldBill.getReturn_num());
    if(bill.getReturn_num()>oldBill.getPurchase_num()||Objects.equals(oldBill.getPurchase_status(),Constants.ALLRETURN)){
      return -2;
    }
    if(Objects.equals(bill.getReturn_num(),oldBill.getPurchase_num())){
      bill.setPurchase_status(Constants.ALLRETURN);
    }
    billMapper.updateReturnNum(bill);
    storeRoomCommodityService.updatePurchase(bill,oldBill.getReturn_num()-bill.getReturn_num());

    return 0;
  }
}
